//
//  OALazyGetterBase.h
//  OASDK_Works
//
//  Created by ChenJie on 3/28/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#ifndef OALazyGetterBase_h
#define OALazyGetterBase_h

#import "metamacros.h"

#define OAGet(_type, _name...) __oa_get_object__(_type, _name)

/*
 * Getter
 */

#define __oa_get_object__(_type, _name...) \
metamacro_if_eq1(metamacro_argcount(_name)) \
    (__oa_getter_p2__(_type, _name)) \
    (__oa_getter_p3__(_type, _name))

#define __oa_get__(...) \
metamacro_if_eq3(metamacro_argcount(__VA_ARGS__)) \
    (__oa_get_object_p3__(__VA_ARGS__)) \
    (metamacro_if_eq4(metamacro_argcount(__VA_ARGS__)) \
        (__oa_get_object_p4__(__VA_ARGS__)) \
        (__oa_get_object_p5__(__VA_ARGS__)) \
    )

#define __oa_getter_p2__(_type, _name) \
- (_type *)_name { \
    if (!metamacro_concat(_, _name)) { \
        _type *object = [[_type alloc] init]; \
        metamacro_concat(_, _name) = object; \
    } \
    return metamacro_concat(_, _name); \
}

#define __oa_getter_p3__(_type, _name, _init) \
__oa_get_object_p3__(_type, _init, _name)

#define __oa_get_object_p3__(_type, _init, _name) \
- (_type *)_name { \
    if (!metamacro_concat(_, _name)) { \
        _type *object = _init(); \
        metamacro_concat(_, _name) = object; \
    } \
    return metamacro_concat(_, _name); \
}

#define __oa_get_object_p4__(_type, _init, _name, _setup) \
- (_type *)_name { \
    if (!metamacro_concat(_, _name)) { \
        _type *object = _init(); \
        _setup(object); \
        metamacro_concat(_, _name) = object; \
    } \
    return metamacro_concat(_, _name); \
}

#define __oa_get_object_p5__(...) \
__oa_get_object_p4__(__VA_ARGS__)


#endif /* OALazyGetterBase_h */
